﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNotebook;
using CNotebook.Sales;

namespace NotebookPortal
{
    public partial class order : System.Web.UI.Page
    {

        string GetNewSeries()
        {
            string _SERIES = "";
            string _DOCTYPE = "2";

            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();
            oSeries.Open();
            if (oSeries.IsAutoNumber(_DOCTYPE) == true)
            {
                _SERIES = oSeries.GetNewSeries(_DOCTYPE);
            }
            else
            {
                _SERIES = "";
            }
            oSeries.Close();

            return _SERIES;
        }
        void BindWarehouse()
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();
            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT * FROM Warehouse";

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            cboWarehouse.ValueField = "Code";
            cboWarehouse.TextField = "Dscription";
            cboWarehouse.DataSource = dt;
            cboWarehouse.DataBind();

        }

        void BindCustomer()
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oConnection.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();
            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT * FROM ADDRESSBOOK WHERE BPType='Customer'";

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            cboCustCode.ValueField = "BPCode";
            cboCustCode.TextField = "BPName";
            cboCustCode.DataSource = dt;
            cboCustCode.DataBind();

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dtPostDate.Value = System.DateTime.Now;
                dtDateNeeded.Value = System.DateTime.Now;

                BindCustomer();
                BindWarehouse();
            }
        }

        protected void btnRegister_Click(object sender, ImageClickEventArgs e)
        {
            OrderManager oManager = new OrderManager();
            OrderUnit oUnit = new OrderUnit();
            string _DRAFT_ORDER_NO = "";

            #region Header Sales Order

            _DRAFT_ORDER_NO = GetNewSeries();
            oUnit.DocumentNumber = _DRAFT_ORDER_NO;
            oUnit.DocumentStatus = "Draft";
            oUnit.PostingDate = Convert.ToDateTime(dtPostDate.Text.ToString());
            oUnit.DueDate = Convert.ToDateTime(dtDateNeeded.Text.ToString());
            oUnit.DocDate = oUnit.PostingDate;

            oUnit.StoreCode = "";
            oUnit.CardCode = cboCustCode.Value.ToString();
            oUnit.CardName = cboCustCode.Text.ToString();
            oUnit.ContactPerson = txtContactPerson.Text;
            oUnit.PaymentTerms = txtPaymentTerms.Text;
            oUnit.Reference = txtReference.Text;
            oUnit.ShipTo = txtShipTo.Text;
            oUnit.BillTo = txtBillTo.Text;
            oUnit.Location = cboWarehouse.Value.ToString();


            oUnit.SalesMan = txtAgentCode.Text;
            
            oUnit.Remarks = "";
            if (txtVATCode.Text != "")
            {
                oUnit.TaxCode = txtVATCode.Text.ToString();
                oUnit.TaxRate = 1.12;
            }
            else
            {
                oUnit.TaxCode = "NON-VAT";
                oUnit.TaxRate = 0;
            }
            if (oUnit.TaxCode != "NON-VAT")
            {
                oUnit.IsVATInclusive = "Y";
            }
            else
            {
                oUnit.IsVATInclusive = "N/A";
            }



            oUnit.GrossAmnt = 0;
            oUnit.VATAmt = 0;
            oUnit.TotalLineDisc = 0;

            oUnit.Discount1 = txtDiscount1.Text;
            oUnit.Discount2 = txtDiscount2.Text;
            oUnit.Discount3 = txtDiscount3.Text;
            oUnit.Discount4 = txtDiscount4.Text;

            oUnit.TotalDiscount = 0;
            oUnit.NetAmount = 0;

            oUnit.DateCreated = System.DateTime.Now;
            oUnit.CreatedBy = Session["ActiveUserID"].ToString();
            oUnit.DateModified = System.DateTime.Now;
            oUnit.ModifiedBy = Session["ActiveUserID"].ToString();

            #endregion 

            oManager.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();
            oManager.Open();

            oManager.AddOrder(oUnit);

            oManager.Close();

            //btnRegister.Visible = false;

            if (Session["DraftSONum"] != null)
            {
                Session.Remove("DraftSONum");
                Session.Add("DraftSONum", _DRAFT_ORDER_NO);
            }
            else
            {
                Session.Add("DraftSONum", _DRAFT_ORDER_NO);
            }

            Response.Redirect("selectorder.aspx");

        }

        protected void cboCustCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            DataTable dt = new DataTable();

            oConnection.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CNS"].ToString();
            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandText = "SELECT * FROM ADDRESSBOOK WHERE BPCode = @BPCode";
            oCommand.Parameters.Add(new SqlParameter("@BPCode", cboCustCode.SelectedItem.Value));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            oConnection.Close();

            DataRow oRow = dt.Rows[0];

            txtContactPerson.Text = oRow["ContactPerson"].ToString();
            txtAgentCode.Text = oRow["SMCode"].ToString();
            txtBillTo.Text = oRow["Address"].ToString();
            txtShipTo.Text = oRow["Address"].ToString();
            txtPaymentTerms.Text = oRow["PaymentTerm"].ToString();
            txtVATCode.Text = oRow["VATCode"].ToString();

            txtDiscount1.Text = oRow["Disc1"] != DBNull.Value ? oRow["Disc1"].ToString() : "0%";
            txtDiscount2.Text = oRow["Disc2"] != DBNull.Value ? oRow["Disc2"].ToString() : "0%";
            txtDiscount3.Text = oRow["Disc3"] != DBNull.Value ? oRow["Disc3"].ToString() : "0%";
            txtDiscount4.Text = oRow["Disc4"] != DBNull.Value ? oRow["Disc4"].ToString() : "0%";

        }
    }
}